package io.renren.modules.contract.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import io.renren.common.utils.*;
import io.renren.modules.project.dao.ProjectApplicantInfoDao;
import io.renren.modules.project.entity.ProjectApplicantInfoEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.contract.entity.ContractPaymentEntity;
import io.renren.modules.contract.service.ContractPaymentService;


/**
 * 付款合同表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-08-02 13:35:37
 */
@RestController
@RequestMapping("/contract/contractpayment")
public class ContractPaymentController {
	@Autowired
	private ContractPaymentService contractPaymentService;
	@Autowired
	private ProjectApplicantInfoDao projectApplicantInfoDao;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("contract:contractpayment:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<ContractPaymentEntity> contractPaymentList = contractPaymentService.queryList(query);
		int total = contractPaymentService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(contractPaymentList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("contract:contractpayment:info")
	public R info(@PathVariable("id") String id){
		ContractPaymentEntity contractPayment = contractPaymentService.queryObject(id);
		
		return R.ok().put("contractPayment", contractPayment);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody ContractPaymentEntity contractPayment) throws ParseException {
		contractPayment.setId(RandomId.createId()+"");
		contractPayment.setCreateTime(new Date());
		if (contractPayment.getContractTimeStr() != null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			contractPayment.setContractTime(sdf.parse(contractPayment.getContractTimeStr()));
		}
		if (StringUtils.isNotBlank(contractPayment.getReportingUnit())){
			String unitId = contractPayment.getReportingUnit();
			//判断输入的项目申请方是否存在
			ProjectApplicantInfoEntity unitCount = projectApplicantInfoDao.queryObject(contractPayment.getReportingUnit());
			//如果不存在则进行新增操作
			if (unitCount == null){
				unitId = UUID.randomUUID().toString().replaceAll("-","");
				ProjectApplicantInfoEntity projectApplicantInfoEntity = new ProjectApplicantInfoEntity();
				projectApplicantInfoEntity.setId(unitId);
				projectApplicantInfoEntity.setApplicantName(contractPayment.getReportingUnit());
				projectApplicantInfoDao.save(projectApplicantInfoEntity);
				contractPayment.setReportingUnitName(contractPayment.getReportingUnit());
				contractPayment.setReportingUnit(unitId);
			}else {
				contractPayment.setReportingUnitName(unitCount.getApplicantName());
				contractPayment.setReportingUnit(unitId);
			}
		}
		contractPaymentService.save(contractPayment);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody ContractPaymentEntity contractPayment){
		if (StringUtils.isNotBlank(contractPayment.getReportingUnit())){
			String unitId = contractPayment.getReportingUnit();
			//判断输入的项目申请方是否存在
			ProjectApplicantInfoEntity unitCount = projectApplicantInfoDao.queryObject(contractPayment.getReportingUnit());
			//如果不存在则进行新增操作
			if (unitCount == null){
				unitId = UUID.randomUUID().toString().replaceAll("-","");
				ProjectApplicantInfoEntity projectApplicantInfoEntity = new ProjectApplicantInfoEntity();
				projectApplicantInfoEntity.setId(unitId);
				projectApplicantInfoEntity.setApplicantName(contractPayment.getReportingUnit());
				projectApplicantInfoDao.save(projectApplicantInfoEntity);
				contractPayment.setReportingUnitName(contractPayment.getReportingUnit());
				contractPayment.setReportingUnit(unitId);
			}else {
				contractPayment.setReportingUnitName(unitCount.getApplicantName());
				contractPayment.setReportingUnit(unitId);
			}
		}
		contractPaymentService.update(contractPayment);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("contract:contractpayment:delete")
	public R delete(@RequestBody String[] ids){
		contractPaymentService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
